// 元组必须明确声明每个成员的类型。
const s: [string, number, boolean] = ['a', 1, false]

// 数组
let a: number[] = [1]

// 元组
let t: [number] = [1]
let arr01 = [1, 'a'] // (string | number)[]
let arr02: [number, string?] = [1]
type myTuple = [number, string, boolean?, bigint?]

// 元组
let arr03: [number, string] = [1, '2']

type NamedNums = [string, ...number[]]

const a1: NamedNums = ['1', 2, 3, 4, 5]
const a2: NamedNums = ['1', 2, 3]

type t1 = [string, number, ...boolean[]]
type t2 = [string, ...boolean[], number]
type t3 = [...boolean[], string, number]

// 元组Tuple可以放置任意数量和类型的成员。
// 但是这样写，也就失去了使用元组和 TypeScript 的意义。
type Tuple = [...any[]]

// 元组的成员可以添加成员名，这个成员名是说明性的，可以任意取名，没有实际作用。
type Color = [red: number, green: number, blue: number]

const c: Color = [244, 123, 222]

type CustomTuple = [string, number]
type Age = CustomTuple[0]

type Tuple01 = [string, number, boolean]
// 联合类型
type TupleEl = Tuple01[number] // string | number | boolean

// 只读元组
type t = Readonly<[number, string]>
const t2: t = [1, '2']
// t2[1] = '123'

type tt = readonly [number, string]

function distanceFromOrigin([x, y]: [number, number]) {
  return Math.sqrt(x ** 2 + y ** 2)
}

let point = [3, 4] as const
// distanceFromOrigin(point) 报错
distanceFromOrigin(point as [number, number])

const arr04 = [1, 2]

function add(x: number, y: number) {
  // todo
}

// add(...arr04) 扩张参数必须具有元组类型或传递给 rest 参数
// console.log(...arr04)

const arr05: [number, number] = [1, 2]
add(...arr05) // OK...

const arr06 = [1, 2] as const
add(...arr06) // OK...
